<?php
/**
 * 格式化静态类
 *
 * @author will
 * @version $Id$
 */

class Format
{
	/**
	 * 去掉字符串中，非 a-z 的字母
	 * @param <string> $str
	 * @return <string>
	 */
	static function alpha($str) {
		$str = preg_replace("/[^a-zA-Z]/" , '' , $str);
		return $str;
	}

	/**
	 * 时间戳格式化
	 * @param <int> $ts
	 * @param <string> $format_type
	 */
	static function time($ts , $format = 'diff') {
		$ts = intval($ts);
		$cur_ts = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		if($ts == 0) {
			$ts = $cur_ts;
		}
		
		switch ($format) {
			case 'diff' :
				$split_ts = abs($cur_ts - $ts);
				
				if($split_ts <= (5*60)) {
					return '刚刚';
				} else if ($split_ts > (5*60) && $split_ts <= (60*60)){
					return round($split_ts / 60) . '分钟前';
				} else if ($split_ts > (60*60) && $split_ts <= (24*60*60)){
					return round($split_ts/3600).'小时前';
				} else if ($split_ts > (24*60*60) && $split_ts <= (7*24*60*60)){
					return round($split_ts/86400).'天前';
				} else if ($split_ts > (7*24*60*60) && $split_ts <= (4*7*24*60*60)){
					return round($split_ts / 604800).'星期前';
				} else {
					return date("Y-m-d H:i",$ts);
				}
			break;
			
			// 使用传进来的格式
			default :
				return date($format , $ts);
			break;
		}
	}
}

# end of script
